package com.yugao.fintech.antelope.system.controller;

import com.yugao.fintech.antelope.auth.AuthApi;
import com.yugao.fintech.antelope.base.log.OperateLog;
import com.yugao.fintech.antelope.base.log.enums.BusinessType;
import com.yugao.fintech.antelope.base.ram.ActionType;
import com.yugao.fintech.antelope.base.ram.RamResource;
import com.yugao.fintech.antelope.system.model.entity.SysUserOnline;
import com.yugao.fintech.antelope.system.service.SysUserOnlineService;
import com.yugao.fintech.framework.cache.core.service.CacheService;
import com.yugao.fintech.framework.core.response.PageData;
import com.yugao.fintech.framework.core.response.Resp;

import javax.annotation.Resource;

/**
 * 在线用户监控 - TODO 移动到授权服务中
 */
@RestController
@RequestMapping("/onlineUser")
public class SysUserOnlineController {
    @Autowired
    private SysUserOnlineService userOnlineService;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private AuthApi authApi;

    @GetMapping("/page")
    @RamResource(action = "sys:onlineUser:query", name = "查询在线用户", type = ActionType.READ)
    public Resp<PageData<SysUserOnline>> page(@RequestParam(value = "ipAddr", required = false) String ipAddr,
                                              @RequestParam(value = "userName", required = false) String userName) {
//        // TODO 这块统计了在线所有用户需要做处理
//        String cacheKey = String.format(OAuth2Cons.CACHE_ACCESS_TOKEN, "*", "*");
//
//        Set<String> keys = cacheService.opsForKey().keys(cacheKey);
//        List<SysUserOnline> userOnlineList = new ArrayList<>();
//        for (String key : keys) {
//            // 获取用户id
//            String[] keySplit = key.split(":");
//            Long userId = Long.parseLong(keySplit[keySplit.length - 2]);
//            String userCacheKey = String.format(OAuth2Cons.CACHE_USER_DETAILS, userId, "1");
//            LoginUser user = cacheService.opsForValue().get(userCacheKey);
//            if (Objects.isNull(user)) {
//                continue;
//            }
//
//            if (StringUtils.isNotEmpty(ipAddr) && StringUtils.isNotEmpty(userName)) {
//                if (StringUtils.equals(ipAddr, user.getLoginIp()) && StringUtils.equals(userName, user.getUserName())) {
//                    userOnlineList.add(userOnlineService.selectOnlineByInfo(ipAddr, userName, user));
//                }
//            } else if (StringUtils.isNotEmpty(ipAddr)) {
//                if (StringUtils.equals(ipAddr, user.getLoginIp())) {
//                    userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipAddr, user));
//                }
//            } else if (StringUtils.isNotEmpty(userName)) {
//                if (StringUtils.equals(userName, user.getUserName())) {
//                    userOnlineList.add(userOnlineService.selectOnlineByuserName(userName, user));
//                }
//            }
//            else {
//                userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
//            }
//        }
//        Collections.reverse(userOnlineList);
//        userOnlineList.removeAll(Collections.singleton(null));
//        return PageUtil.getDataTable(userOnlineList);
        return Resp.ok();
    }

    /**
     * 强退用户
     */
    @RamResource(action = "sys:onlineUser:forceLogout", name = "强退在线用户", type = ActionType.WRITE)
    @OperateLog(summary = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/forceLogout")
    public Resp<?> forceLogout(@RequestParam(value = "userId") Long userId) {
        authApi.removeAccessTokenByUserId(userId);
        return Resp.ok();
    }

    /**
     * 强退用户
     */
    @RamResource(action = "sys:onlineUser:forceLogout", name = "强退在线用户", type = ActionType.WRITE)
    @OperateLog(summary = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/forceLogoutBatch")
    public Resp<?> forceLogoutBatch(@RequestParam(value = "userIds") String userIdStr) {
        userOnlineService.forceLogoutBatch(userIdStr);
        return Resp.ok();
    }


}